package com.aj.service;

import com.aj.Interface.OrderSettingService;
import com.aj.dao.OrderSettingDao;
import com.aj.pojo.OrderSetting;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 添加预约设置
     * @param orderSettingList
     */
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList!=null && orderSettingList.size()>0){

        //遍历集合
        for (OrderSetting orderSetting : orderSettingList) {
            //判断日期是否存在
          long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());

            if (count>0){
                //日期存在,更新数据
                orderSettingDao.update(orderSetting);
            }else {
                //日期不存在,添加数据
                orderSettingDao.add(orderSetting);
            }
        }

        }
    }

    /**
     * 根据月份获得当前月的预约设置
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String databegin =date+"-01";
        String dataend =date+"-31";
        System.out.println(databegin);
            Map map = new HashMap();
            map.put("databegin", databegin);
            map.put("dataend", dataend);
        List<OrderSetting> lists = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting list : lists) {
            Map ordermap = new HashMap();
            ordermap.put("date", list.getOrderDate().getDate());
            ordermap.put("number",list.getNumber());
            ordermap.put("reservations",list.getReservations());
            data.add(ordermap);
        }
        return data;
    }


    /**
     * 更新预约设置的人数
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count>0){
            orderSettingDao.update(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }

    }
}
